home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / Players / Texture3D / Textures.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  14.7 KB  |  420 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Textures.cpp
  3. //
  4. // Desc: DirectShow sample code - uses the Direct3D Textures Tutorial05 as 
  5. //       a base to create an application that uses DirectShow to draw a video 
  6. //       on a DirectX 8.0 Texture surface.
  7.  
  8. // Copyright (c) 2000-2001 Microsoft Corporation.  All rights reserved.
  9. //-----------------------------------------------------------------------------
  10.  
  11.  
  12. #include "textures.h"
  13. #include "resource.h"
  14.  
  15. #pragma warning( disable : 4100 4238)
  16.  
  17. //-----------------------------------------------------------------------------
  18. // Global variables
  19. //-----------------------------------------------------------------------------
  20. LPDIRECT3D8             g_pD3D       = NULL; // Used to create the D3DDevice
  21. LPDIRECT3DDEVICE8       g_pd3dDevice = NULL; // Our rendering device
  22. LPDIRECT3DVERTEXBUFFER8 g_pVB        = NULL; // Buffer to hold vertices
  23. LPDIRECT3DTEXTURE8      g_pTexture   = NULL; // Our texture
  24. HINSTANCE               hInstance    = 0;
  25.  
  26. // A structure for our custom vertex type. We added texture coordinates
  27. struct CUSTOMVERTEX
  28. {
  29.     D3DXVECTOR3 position; // The position
  30.     D3DCOLOR    color;    // The color
  31.     FLOAT       tu, tv;   // The texture coordinates
  32. };
  33.  
  34. // Our custom FVF, which describes our custom vertex structure
  35. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
  36. #define CLASSNAME   TEXT("DShow Texture3D Sample")
  37.  
  38. // Function prototypes
  39. void AddAboutMenuItem(HWND hWnd);
  40. LRESULT CALLBACK AboutDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  41.  
  42. //-----------------------------------------------------------------------------
  43. // Name: InitD3D()
  44. // Desc: Initializes Direct3D
  45. //-----------------------------------------------------------------------------
  46. HRESULT InitD3D( HWND hWnd )
  47. {
  48.     HRESULT hr;
  49.  
  50.     // Create the D3D object.
  51.     if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
  52.         return E_FAIL;
  53.  
  54.     // Get the current desktop display mode, so we can set up a back
  55.     // buffer of the same format
  56.     D3DDISPLAYMODE d3ddm;
  57.     if ( FAILED( hr = g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
  58.     {
  59.         Msg(TEXT("Could not read adapter display mode!  hr=0x%x"), hr);
  60.         return hr;
  61.     }
  62.  
  63.     // Set up the structure used to create the D3DDevice. Since we are now
  64.     // using more complex geometry, we will create a device with a zbuffer.
  65.     D3DPRESENT_PARAMETERS d3dpp;
  66.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  67.     d3dpp.Windowed               = TRUE;
  68.     d3dpp.SwapEffect             = D3DSWAPEFFECT_COPY_VSYNC;
  69.     d3dpp.BackBufferFormat       = d3ddm.Format;
  70.     d3dpp.EnableAutoDepthStencil = TRUE;
  71.     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  72.  
  73.     // Create the D3DDevice
  74.     hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  75.                                D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED,
  76.                                &d3dpp, &g_pd3dDevice );                                     
  77.     if (FAILED(hr))                                      
  78.     {
  79.         Msg(TEXT("Could not create the D3D device!  hr=0x%x\r\n\r\n")
  80.             TEXT("This sample is attempting to create a buffer that might not\r\n")
  81.             TEXT("be supported by your video card in its current mode.\r\n\r\n")
  82.             TEXT("You may want to reduce your screen resolution or bit depth\r\n")
  83.             TEXT("and try to run this sample again."), hr);
  84.         return hr;
  85.     }
  86.  
  87.     // Turn off culling
  88.     hr = g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  89.  
  90.     // Turn off D3D lighting
  91.     hr = g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
  92.  
  93.     // Turn on the zbuffer
  94.     hr = g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
  95.  
  96.     // Set texture states
  97.     hr = g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  98.     hr = g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  99.     hr = g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  100.     hr = g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
  101.  
  102.     // Add filtering
  103.     hr = g_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  104.     hr = g_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  105.  
  106.     return hr;
  107. }
  108.  
  109.  
  110.  
  111. //-----------------------------------------------------------------------------
  112. // Name: InitGeometry()
  113. // Desc: Create the textures and vertex buffers
  114. //-----------------------------------------------------------------------------
  115. HRESULT InitGeometry()
  116. {
  117.     HRESULT hr;
  118.  
  119.     // DShow: Set up filter graph with our custom renderer.
  120.     if( FAILED( InitDShowTextureRenderer(g_pTexture ) ) )
  121.         return E_FAIL;
  122.  
  123.     // Create the vertex buffer.
  124.     if( FAILED( hr = g_pd3dDevice->CreateVertexBuffer( 50*2*sizeof(CUSTOMVERTEX),
  125.                                       0, D3DFVF_CUSTOMVERTEX,
  126.                                       D3DPOOL_DEFAULT, &g_pVB ) ) )
  127.     {
  128.         Msg(TEXT("Could not create a vertex buffer!  hr=0x%x"), hr);
  129.         return E_FAIL;
  130.     }
  131.  
  132.     // Fill the vertex buffer. We are setting the tu and tv texture
  133.     // coordinates, which range from 0.0 to 1.0
  134.     CUSTOMVERTEX* pVertices;
  135.     if ( FAILED( hr = g_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ) ) )
  136.     {
  137.         Msg(TEXT("Could not lock the vertex buffer!  hr=0x%x"), hr);
  138.         return E_FAIL;
  139.     }
  140.  
  141.     for( DWORD i=0; i<50; i++ )
  142.     {
  143.         FLOAT theta = (2*D3DX_PI*i)/(50-1);
  144.  
  145.         pVertices[2*i+0].position = D3DXVECTOR3( sinf(theta),-1.0f, cosf(theta) );
  146.         pVertices[2*i+0].color    = 0xffffffff;
  147.         pVertices[2*i+0].tu       = ((FLOAT)i)/(50-1);
  148.         pVertices[2*i+0].tv       = 1.0f;
  149.  
  150.         pVertices[2*i+1].position = D3DXVECTOR3( sinf(theta), 1.0f, cosf(theta) );
  151.         pVertices[2*i+1].color    = 0xff808080;
  152.         pVertices[2*i+1].tu       = ((FLOAT)i)/(50-1);
  153.         pVertices[2*i+1].tv       = 0.0f;
  154.     }
  155.  
  156.     g_pVB->Unlock();
  157.     return S_OK;
  158. }
  159.  
  160.  
  161.  
  162. //-----------------------------------------------------------------------------
  163. // Name: Cleanup()
  164. // Desc: Releases all previously initialized objects
  165. //-----------------------------------------------------------------------------
  166. VOID Cleanup()
  167. {
  168.     CleanupDShow();
  169.  
  170.     if( g_pTexture != NULL )   
  171.         g_pTexture->Release();
  172.         
  173.     if( g_pVB != NULL )
  174.         g_pVB->Release();
  175.         
  176.     if( g_pd3dDevice != NULL ) 
  177.         g_pd3dDevice->Release();
  178.         
  179.     if( g_pD3D != NULL )
  180.         g_pD3D->Release();
  181. }
  182.  
  183.  
  184.  
  185. //-----------------------------------------------------------------------------
  186. // Name: SetupMatrices()
  187. // Desc: Sets up the world, view, and projection transform matrices.
  188. //-----------------------------------------------------------------------------
  189. VOID SetupMatrices()
  190. {
  191.     HRESULT hr;
  192.  
  193.     // For our world matrix, we will just leave it as the identity
  194.     D3DXMATRIX matWorld;
  195.     D3DXMatrixIdentity( &matWorld );
  196.     D3DXMatrixRotationX( &matWorld, timeGetTime()/2000.0f );
  197.     hr = g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  198.     if (FAILED(hr))                                      
  199.     {
  200.         Msg(TEXT("Could not set D3DTS_WORLD transform!  hr=0x%x"), hr);
  201.     }
  202.  
  203.     // Set up our view matrix. A view matrix can be defined given an eye point,
  204.     // a point to lookat, and a direction for which way is up. Here, we set the
  205.     // eye five units back along the z-axis and up three units, look at the
  206.     // origin, and define "up" to be in the y-direction.
  207.     D3DXMATRIX matView;
  208.     D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 3.0f,-5.0f ),
  209.                                   &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
  210.                                   &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
  211.     hr = g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  212.     if (FAILED(hr))                                      
  213.     {
  214.         Msg(TEXT("Could not set D3DTS_VIEW transform!  hr=0x%x"), hr);
  215.     }
  216.  
  217.     // For the projection matrix, we set up a perspective transform (which
  218.     // transforms geometry from 3D view space to 2D viewport space, with
  219.     // a perspective divide making objects smaller in the distance). To build
  220.     // a perpsective transform, we need the field of view (1/4 pi is common),
  221.     // the aspect ratio, and the near and far clipping planes (which define at
  222.     // what distances geometry should be no longer be rendered).
  223.     D3DXMATRIX matProj;
  224.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
  225.     hr = g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  226.     if (FAILED(hr))                                      
  227.     {
  228.         Msg(TEXT("Could not set D3DTS_PROJECTION transform!  hr=0x%x"), hr);
  229.     }
  230. }
  231.  
  232.  
  233.  
  234. //-----------------------------------------------------------------------------
  235. // Name: Render()
  236. // Desc: Draws the scene
  237. //-----------------------------------------------------------------------------
  238. VOID Render()
  239. {
  240.     // Clear the backbuffer and the zbuffer
  241.     g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  242.                          D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
  243.  
  244.     // Begin the scene
  245.     g_pd3dDevice->BeginScene();
  246.  
  247.     // Setup the world, view, and projection matrices
  248.     SetupMatrices();
  249.  
  250.     // Setup our texture. Using textures introduces the texture stage states,
  251.     // which govern how textures get blended together (in the case of multiple
  252.     // textures) and lighting information. In this case, we are modulating
  253.     // (blending) our texture with the diffuse color of the vertices.
  254.     g_pd3dDevice->SetTexture( 0, g_pTexture );
  255.  
  256.     // Render the vertex buffer contents
  257.     g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
  258.     g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
  259.     g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2*50-2 );
  260.  
  261.     // End the scene
  262.     g_pd3dDevice->EndScene();
  263.  
  264.     // Present the backbuffer contents to the display
  265.     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  266.  
  267.     // Check to see if we need to restart the movie
  268.     CheckMovieStatus();
  269. }
  270.  
  271.  
  272.  
  273. //-----------------------------------------------------------------------------
  274. // Name: MsgProc()
  275. // Desc: The window's message handler
  276. //-----------------------------------------------------------------------------
  277. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  278. {
  279.     switch( msg )
  280.     {
  281.         case WM_DESTROY:
  282.             PostQuitMessage( 0 );
  283.             return 0;
  284.  
  285.         case WM_KEYUP:
  286.         {
  287.             // Close the app if the ESC key is pressed
  288.             if (wParam == VK_ESCAPE)
  289.                 PostMessage(hWnd, WM_CLOSE, 0, 0);
  290.         }
  291.         break;
  292.  
  293.         case WM_SYSCOMMAND:
  294.         {
  295.             switch (wParam)
  296.             {
  297.                 case ID_HELP_ABOUT:
  298.                     DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, 
  299.                              (DLGPROC) AboutDlgProc);
  300.                     return 0;
  301.             }
  302.         }
  303.         break;
  304.     }
  305.  
  306.     return DefWindowProc( hWnd, msg, wParam, lParam );
  307. }
  308.  
  309.  
  310. //-----------------------------------------------------------------------------
  311. // Name: AboutDlgProc()
  312. // Desc: Message handler for About box
  313. //-----------------------------------------------------------------------------
  314.  
  315. LRESULT CALLBACK AboutDlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  316. {
  317.     switch (message)
  318.     {
  319.         case WM_INITDIALOG:
  320.             return TRUE;
  321.  
  322.         case WM_COMMAND:
  323.             if (wParam == IDOK)
  324.             {
  325.                 EndDialog(hWnd, TRUE);
  326.                 return TRUE;
  327.             }
  328.             break;
  329.     }
  330.     return FALSE;
  331. }
  332.  
  333. //-----------------------------------------------------------------------------
  334. // Name: AddAboutMenuItem()
  335. // Desc: Adds a menu item to the end of the app's system menu
  336. //-----------------------------------------------------------------------------
  337. void AddAboutMenuItem(HWND hWnd)
  338. {
  339.     // Add About box menu item
  340.     HMENU hwndMain = GetSystemMenu(hWnd, FALSE);
  341.  
  342.     // Add separator
  343.     BOOL rc = AppendMenu(hwndMain, MF_SEPARATOR, 0, NULL);
  344.  
  345.     // Add menu item
  346.     rc = AppendMenu(hwndMain, MF_STRING | MF_ENABLED, 
  347.                     ID_HELP_ABOUT, 
  348.                     TEXT("About Texture3D...\0"));
  349. }
  350.  
  351. //-----------------------------------------------------------------------------
  352. // Name: WinMain()
  353. // Desc: The application's entry point
  354. //-----------------------------------------------------------------------------
  355. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR lpCmdLine, INT nCmdShow)
  356. {
  357.     // Initialize COM
  358.     CoInitialize (NULL);
  359.  
  360.     // Register the window class
  361.     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
  362.                       GetModuleHandle(NULL), 
  363.                       LoadIcon(hInst, MAKEINTRESOURCE(IDI_TEXTURES)), 
  364.                       NULL, NULL, NULL,
  365.                       CLASSNAME, NULL };
  366.     RegisterClassEx( &wc );
  367.     hInstance = hInst;
  368.  
  369.     // Create the application's window
  370.     HWND hWnd = CreateWindow( CLASSNAME, TEXT("DShow Texture3D Sample"),
  371.                               WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
  372.                               GetDesktopWindow(), NULL, wc.hInstance, NULL );
  373.  
  374.     // Add a menu item to the app's system menu
  375.     AddAboutMenuItem(hWnd);
  376.  
  377.     // Initialize Direct3D
  378.     if( SUCCEEDED( InitD3D( hWnd ) ) )
  379.     {
  380.         // Create the scene geometry
  381.         if( SUCCEEDED( InitGeometry() ) )
  382.         {
  383.             // Show the window
  384.             ShowWindow( hWnd, SW_SHOWDEFAULT );
  385.             UpdateWindow( hWnd );
  386.  
  387.             // Enter the message loop
  388.             MSG msg;
  389.             ZeroMemory( &msg, sizeof(msg) );
  390.             while( msg.message!=WM_QUIT )
  391.             {
  392.                 if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  393.                 {
  394.                     TranslateMessage( &msg );
  395.                     DispatchMessage( &msg );
  396.                 }
  397.                 else
  398.                 {
  399.                     Render();
  400.  
  401.                     // We're not attempting to achieve a fast D3D frame rate
  402.                     // in this sample, so let's just sleep for a while to 
  403.                     // reduce CPU utilization.  Otherwise, this app will use
  404.                     // as much CPU horsepower as possible, since the PeekMessage()
  405.                     // loop will run until the user closes the application.
  406.                     Sleep(25);
  407.                 }
  408.             }
  409.         }
  410.     }
  411.  
  412.     // Clean up everything and exit the app
  413.     Cleanup();
  414.     UnregisterClass( CLASSNAME, wc.hInstance );
  415.     CoUninitialize();
  416.     return 0L;
  417. }
  418.  
  419.  
  420.